/*
  新建8个模块   approval attendance employee(员工模块: 员工列表 添加员工) ...

  页面有了 配规则
  approval/index.vue  审批模块下的第一个页面   =>   /approval
  employee/index.vue  员工模块下的第一个页面   =>   /employee

*/

import Vue from 'vue'
import Router from 'vue-router'
import employeeRouter from './modules/employee'
import approvalRouter from './modules/approval'
import attendanceRouter from './modules/attendance'
import departmentRouter from './modules/department'
import permissionRouter from './modules/permission'
import roleRouter from './modules/role'
import salaryRouter from './modules/salary'
import socialRouter from './modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout' // 直接加载页面（不是懒加载）
// 懒加载： 提高页面加载的效率 看到的页面才去加载 没看到的页面先不去加载

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
/*
  这边的路由规则将来会被v-for生成一个个菜单项
  路由规则上的hidden 会影响菜单项的生成   true 不显示菜单项
*/
export const constantRoutes = [
  // 登录页
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 404页面
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 首页（一级路由页面 内嵌二级页面）
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard', // 给当前路由规则加个名字而已  将来配合编程式导航跳转利用name
        component: () => import('@/views/dashboard/index'),
        meta: { title: '主页', icon: 'dashboard' } // 和菜单有关
      }
    ]
  }

  // 404 页面必须在所有规则的最后 !!!
  // { path: '*', redirect: '/404', hidden: true }
]

/*
  router 路由实例
  router.addRoutes => 给现有的路由实例添加规则的
*/
export const asyncRoutes = [
  // 如果路由规则本身一直存在=>不会发生变化=>静态路由
  // 这些页面是将来需要控制权限的=>有的人有 有的人没有=>动态路由
  departmentRouter,
  roleRouter,
  employeeRouter,
  permissionRouter,
  attendanceRouter,
  approvalRouter,
  salaryRouter,
  socialRouter
]

// new VueRouter({
//   routes: []
// })

const createRouter = () => {
  return new Router({
    mode: 'history', // 需要后端支持
    scrollBehavior: () => {
      return {
        y: 0
      }
    }, // 创建路由实例时 指定每次切换页面 都要滚动到顶部
    routes: constantRoutes
  })
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由 => 将router中的路由规则全部清空
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
